'use strict';

angular.module('swanApp').directive('ngUnique', ['$http', function ($http) {
    return {
        require: 'ngModel',
        link: function (scope, elem, attrs, ctrl) {
            elem.on('blur', function (evt) {
                scope.$apply(function () {
                    var entity=attrs.ngUnique;
                    $http({
                        method: 'POST',
                        url: 'api/'+entity+'/checkname',
                        params : {
                            name:elem.val()
                        }
                    }).success(function(data, status, headers, config) {
                        ctrl.$setValidity('unique', data);
                    });
                });
            });
        }
    }}
    ]);
